﻿using System;
using ICSharpCode.TextEditor.Document;

namespace ICSharpCode.TextEditor.Extend
{
    /// <summary>
    /// TextEditor文本查找扩展类
    /// </summary>
    public class TextEditorQueryExtend
    {
        private int startindex = 0;
        private string lastText = string.Empty;

        private TextEditorControl textEditorControl;

        public TextEditorQueryExtend(TextEditorControl textEditorControl)
        {
            this.textEditorControl = textEditorControl;
        }


        /// <summary>
        /// 文本定位(查找)
        /// </summary>
        /// <param name="text">查找文本</param>
        /// <param name="circulate">是否循环查找</param>
        /// <param name="ignoreCase">是否区分大小写，区分=true，不区分=false</param>
        public Tuple<int, int> TextPosition(string text, bool circulate = true, bool ignoreCase = false)
        {
            try
            {
                if (string.IsNullOrEmpty(lastText) || lastText != text)
                {
                    this.startindex = 0;
                    lastText = text;
                }

                StringComparison stringComparison = StringComparison.CurrentCultureIgnoreCase;//默认不区分大小写
                if (ignoreCase) stringComparison = StringComparison.CurrentCulture;  //CurrentCultureIgnoreCase


                //设置选择的文本。
                var offset = this.textEditorControl.Text.IndexOf(text, this.startindex, stringComparison);

                if (offset >= 0)
                {
                    this.startindex = offset + text.Length;//为查找下一个做准备
                    var start = this.textEditorControl.Document.OffsetToPosition(offset);
                    var end = this.textEditorControl.Document.OffsetToPosition(offset + text.Length);
                    this.textEditorControl.ActiveTextAreaControl.SelectionManager.SetSelection(new DefaultSelection(this.textEditorControl.Document, start, end));

                    //滚动到选择的位置。
                    this.textEditorControl.ActiveTextAreaControl.Caret.Position = end;
                    this.textEditorControl.ActiveTextAreaControl.TextArea.ScrollToCaret();

                    return new Tuple<int, int>(start.Line + 1, start.Column);
                }
                else if (this.textEditorControl.Text.IndexOf(text, stringComparison) > 0)
                {
                    if (circulate)
                    {
                        this.startindex = 0;//循环查找
                        return TextPosition(text, circulate, ignoreCase);
                    }
                }

            }
            catch (Exception)
            {

            }
            return new Tuple<int, int>(0, 0);
        }

        /// <summary>
        /// 计数
        /// </summary>
        /// <param name="text">查找文本</param>
        /// <param name="ignoreCase">是否区分大小写，区分=true，不区分=false</param>
        /// <returns></returns>
        public int GetTextCount(string text, bool ignoreCase = false)
        {
            int startindex = 0;

            int count = 0;

            StringComparison stringComparison = StringComparison.CurrentCultureIgnoreCase;//默认不区分大小写
            if (ignoreCase) stringComparison = StringComparison.CurrentCulture;  //CurrentCultureIgnoreCase

            while (true)
            {
                //设置选择的文本。
                var offset = this.textEditorControl.Text.IndexOf(text, startindex, stringComparison);
                if (offset >= 0)
                {
                    count++;
                    startindex = offset + text.Length;//为查找下一个做准备                    
                }
                else
                {
                    break;
                }
                //else if (this.textEditorControl.Text.IndexOf(text, stringComparison) > 0)
                //{
                //    break;
                //}
            }
            return count;
        }

    }
}
